<template>
  <view class="applySeal">
    <electronSeal ref="electronSeal" @changeSealType="changeSealType"></electronSeal>
    <u-no-network></u-no-network>
  </view>
</template>

<script>
let eventChannel
import { mapGetters, mapMutations } from 'vuex'
import electronSeal from './components/electronSeal/electronSeal'

export default {
  computed: {
    ...mapGetters(['userInfo', 'api', 'userToken'])
  },
  components: {
    electronSeal
  },
  data() {
    return {
      sealType: 'electron', //electron电子章，entity实体章
      picker: {
        show: false,
        type: '', //当前选择的时间字段
        data: []
      }
    }
  },
  //监听头部导航事件
  onNavigationBarButtonTap(e) {
    this.$refs.electronSeal.navigationBarButtonTap(e)
  },
  onReady() {
    this.$refs.electronSeal.readyFn()
  },
  onLoad() {
    uni.setNavigationBarTitle({title: this.$t('申请用章')})
    //电子印章申请参数
    eventChannel = this.getOpenerEventChannel()
    eventChannel.on('pagePramas', (data) => {
      this.$nextTick(() => {
        this.$refs.electronSeal.echo(data)
      })
    })
    //接收参数
    this.busFn()
  },
  methods: {
    busFn() {
      //接收审批人
      uni.$off('data_audit')
      uni.$on('data_audit', (res) => {
        this.$refs.electronSeal.data_audit(res)
      })
    },
    //查询用户所属组织列表
    getDepartmentsByLogin() {
      this.$u
        .post(this.api.GLOBAL_auth_department_getDepartmentsByLogin, {
          page: false
        })
        .then((res) => {
          if (res.code === 200) {
            this.picker.data = res.data
            this.picker.show = true
          }
        })
    },
    //取消选取组织
    pickerCancel() {
      if (this.sealType === 'entity') {
        this.$refs.entitySeal.pickerCancel()
      }
    },
    //确认选取组织
    pickerConfirm(e) {
      if (this.sealType === 'entity') {
        this.$refs.entitySeal.pickerConfirm(this.picker.data[e].id)
      }
    },
    //切换类型
    changeSealType(sealType) {
      this.sealType = sealType
    },
    //非量子选取文件
    notQssSeal() {
      /*
      选择文件并上传
      currentWebview=当前窗口，仅app端需要传，且必传
      url=上传服务器地址，必填
      name=上传文件的key(选填，默认为file)
      header=请求头(选填)
      filetypes：为支持选取的格式数组默认["jpg","jpeg","png","rar","txt","zip","doc","ppt","xls","pdf","docx","xlsx","mp4","wmv","avi","mp3","m4a","wma",
            "JPG","JPEG","PNG","RAR","TXT","ZIP","DOC","PPT","XLS","PDF","DOCX","XLSX","MP4","WMV","AVI","MP3","M4A","WMA"]
      */
      this.$refs.lFile.upload({
        // #ifdef APP-PLUS
        // nvue页面使用时请查阅nvue获取当前webview的api，当前示例为vue窗口
        currentWebview: this.$mp.page.$getAppWebview(),
        // #endif
        url: uni.getStorageSync('baseUrl') + this.api.GLOBAL_file_fileInfo_uploadAndGetFile,
        //默认file,上传文件的key
        name: 'file',
        header: {
          Authorization: this.userToken,
          us: 'app'
        },
        filetypes: [
          'jpg',
          'jpeg',
          'png',
          'rar',
          'txt',
          'zip',
          'doc',
          'ppt',
          'xls',
          'pdf',
          'docx',
          'xlsx',
          'JPG',
          'JPEG',
          'PNG',
          'RAR',
          'TXT',
          'ZIP',
          'DOC',
          'PPT',
          'XLS',
          'PDF',
          'DOCX',
          'XLSX'
        ],
        crossDomain: {
          //为了解决ios跨域问题
          name: '检测跨域',
          src1: uni.getStorageSync('baseUrl') + this.api.GLOBAL_auth_org_getOrgsByPhone,
          src2: uni.getStorageSync('baseUrl') + this.api.GLOBAL_file_fileInfo_uploadBase64
        }
      })
    },
    //上传文件成功
    upSuccess(res) {
      let mun = res.fileName.lastIndexOf('.')
      let sliceName = res.fileName.slice(mun + 1, res.fileName.length)
      let obj = {
        id: res.data.id.fileId,
        file: res.data.id,
        type: sliceName
      }
      if (this.sealType === 'entity') {
        this.$refs.entitySeal.upSuccess(obj)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.applySeal {
  background-color: #fff;

  .submissionbtn {
    width: 100%;
    display: flex;
    justify-content: center;
    margin-top: 40rpx;
  }
}
</style>
